home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / YICN23.ZIP / UNITS / YAKWIN.CPP < prev   
C/C++ Source or Header  |  1993-02-21  |  10KB  |  374 lines

  1. #include <iostream.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. #include "yakwin.h"
  5. #include "yakMouse.h"
  6. #include "xlib.h"
  7. #include "xpbitmap.h"
  8. #include "xrect.h"
  9. #include "xtext.h"
  10. #include "xline.h"
  11. #include <conio.h>
  12.  
  13. extern yakMouse mouse;
  14.  
  15. yakWindow * yakWindow::killPointer = NULL;
  16.  
  17. //yakWindowPane functions follow------------------------------------->
  18. yakWindowPane::yakWindowPane(word x1, word y1, word x2, word y2)
  19. {
  20.   width = x2-x1;
  21.   bWidth = (width + (((4-(width % 4)) == 4) ? 0 : (4-(width%4))))/4;  //smallest mult of 4 > width!
  22.   x=x1; y=y1; height = y2-y1;
  23.   myGraphicData = NULL;
  24. }
  25.  
  26. void yakWindowPane::save(word x1, word y1, word ioffset)
  27. {
  28.   if (myGraphicData)
  29.   {
  30.     delete myGraphicData;
  31.     myGraphicData = NULL;
  32.   }
  33.   x = x1; y = y1; myOffset = ioffset;
  34.   if (!myGraphicData)
  35.     myGraphicData = new byte[bWidth*4*height + 4];
  36.   x_get_pbm(x1, y1, bWidth, height, myOffset, myGraphicData);
  37. }
  38.  
  39. void yakWindowPane::save(void)
  40. {
  41.   if (myGraphicData)
  42.   {
  43.     delete myGraphicData;
  44.     myGraphicData = NULL;
  45.   }
  46.   if (!myGraphicData)
  47.     myGraphicData = new byte[bWidth*4*height + 4];
  48.   if (myGraphicData == NULL)
  49.     exit(EXIT_FAILURE);
  50.  
  51.   x_get_pbm(x, y, bWidth, height, myOffset, myGraphicData);
  52. }
  53.  
  54. void yakWindowPane::restore(word ioffset)
  55. {
  56.   x_put_pbm(x, y, ioffset, myGraphicData);
  57. }
  58.  
  59. void yakWindowPane::restore(void)
  60. {
  61.   if (myGraphicData == NULL)
  62.     exit(EXIT_FAILURE);
  63.   x_put_pbm(x, y, VisiblePageOffs, myGraphicData);
  64.   if (VisiblePageOffs != HiddenPageOffs)
  65.     x_put_pbm(x, y, HiddenPageOffs, myGraphicData);
  66. }
  67.  
  68. //yakWindow definitions follow--------------------------------------->
  69.  
  70. yakWindow * yakWindow::topWindow = NULL;
  71. yakWindow * yakWindow::bottomWindow = NULL;
  72. yakWindow * yakWindow::activeWindow = NULL;
  73.  
  74. yakWindow::yakWindow(int x1, int y1, int x2, int y2) :
  75. yakWindowPane(x1, y1, x2, y2)
  76. {
  77.   myOffset = VisiblePageOffs;
  78.   textColor = 15;
  79.   titleBarColor = 63;
  80.   boxColor = 0;
  81.   title[0]= 0;
  82.   myFlags = isSizeable | isDraggable | isCloseable;
  83. }
  84.  
  85.  
  86. void yakWindow::open(void) //can only open on top...
  87. {
  88.   mouse.hide();
  89.   if (bottomWindow == NULL)  //then we're the bottom window!
  90.   {
  91.     bottomWindow = this;
  92.     prevWindow = NULL;
  93.     nextWindow = NULL;
  94.   }
  95.   if (topWindow) //if there is a top window, set up accordingly.
  96.   {
  97.     topWindow->showDeActivated();
  98.     topWindow->nextWindow = this;
  99.     prevWindow = topWindow;
  100.     nextWindow = NULL;
  101.   }
  102.   topWindow = this;
  103.   save();
  104.   draw();
  105.   showActivated();
  106.   activeWindow = this;
  107.   mouse.show();
  108. }
  109.  
  110. void yakWindow::close(void)  //can only close on top too.
  111. {
  112.   mouse.hide();
  113.   if (topWindow != this)
  114.     shuffleToTop();       //now we know we're topWindow.
  115.   topWindow = prevWindow; //which should be null if we're on bottom.
  116.   if (bottomWindow == this)
  117.     bottomWindow = NULL;  //we're closing the last window.
  118.   if (topWindow)          //if there's still a top window,
  119.     topWindow->nextWindow = NULL;  //make sure it doesn't think there's a next!
  120.   restore();
  121.   if (myGraphicData)
  122.   {
  123.     delete myGraphicData;
  124.     myGraphicData = NULL;
  125.   }
  126.   activeWindow = topWindow;
  127.   topWindow->showActivated();
  128.   if (myFlags & isTemporary)
  129.     killPointer = this;
  130.   mouse.show();
  131. }
  132.  
  133. void yakWindow::shuffleToTop(void)
  134. {
  135.   mouse.hide(); //so we don't disrupt everything!
  136.   yakWindow * myYakWindowPointer = topWindow;
  137.   if (myYakWindowPointer == NULL)
  138.   {
  139.     mouse.show();
  140.     return; //if there're no windows,  we can't do much!
  141.   }
  142.   if (myYakWindowPointer == this)
  143.   {
  144.     mouse.show();
  145.     return; //we're already on top.
  146.   }
  147.   while ((myYakWindowPointer != this) && (myYakWindowPointer != NULL))
  148.   {
  149.     myYakWindowPointer->restore();   //restore screen from top window down.
  150.     myYakWindowPointer = myYakWindowPointer->prevWindow;
  151.   }
  152.   if (myYakWindowPointer == NULL)
  153.   {
  154.     drawAll();
  155.     open(); //put this window on top...
  156.     mouse.show();
  157.     return; //this window wasn't in the list!
  158.   }
  159.   restore(); //now restore this window.
  160.   if (prevWindow)  //if we're not the bottom
  161.     prevWindow->nextWindow = nextWindow;  //eliminate this window from list
  162.   else
  163.     bottomWindow = nextWindow;        //we are the bottom, so make next
  164.   if (nextWindow)
  165.     nextWindow->prevWindow = prevWindow;
  166.   myYakWindowPointer = nextWindow; //now look at next window
  167.   while (myYakWindowPointer != topWindow)
  168.   {
  169.     myYakWindowPointer->save();
  170.     myYakWindowPointer->draw();
  171.     myYakWindowPointer = myYakWindowPointer->nextWindow;
  172.   }
  173.   myYakWindowPointer->save();  //now save top window
  174.   myYakWindowPointer->draw();
  175.   open();  //now add our new window.
  176.   mouse.show();
  177. }
  178.  
  179. void yakWindow::drawAll(void)
  180. {
  181.   yakWindow * myYakWindowPointer = bottomWindow;
  182.   if (myYakWindowPointer)
  183.     while (myYakWindowPointer != NULL)
  184.     {
  185.       myYakWindowPointer->save();
  186.       myYakWindowPointer->draw();
  187.       myYakWindowPointer = myYakWindowPointer->nextWindow;
  188.     }
  189. }
  190.  
  191. void yakWindow::draw(word offset)
  192. {
  193.   x = x;
  194.   y = y;
  195.   x_rect_fill(x, y, x+width, y+height, offset, 10);
  196.   x_rect_fill(x+2, y+2, x+width-2, y+height-2, offset, boxColor);
  197.   x_line(x,y, x+width-1, y, 15, offset); //top
  198.   x_line(x,y, x, y+height-1, 15, offset); //left
  199.   x_line(x,y+height-1, x+width-1, y+height-1, 5, offset); //bottom
  200.   x_line(x+width-1,y, x+width-1, y+height-1, 5, offset); //right
  201.   x_line(x+width - 2, y + height - 4, x+width-1, y+height - 4, 0, offset); //size box
  202.   x_line(x+width - 4, y+height - 2, x+width-4, y+height-1, 0, offset); //size box
  203.   x_line(x+3, y, x+3, y+2, 0, offset); //close box
  204.   x_line(x, y+3, x+2, y+3, 0, offset); //close box
  205.   showDeActivated(offset);
  206. }
  207.  
  208. void yakWindow::draw(void)
  209. {
  210.   draw(VisiblePageOffs);
  211.   if (VisiblePageOffs != HiddenPageOffs)
  212.     draw(HiddenPageOffs);
  213. }
  214.  
  215. void yakWindow::showActivated(word offset)
  216. {
  217.   x_rect_fill(x+2, y+2, x+width - 2, y+2+CharHeight, offset, titleBarColor);
  218.   x_bgprintf(x+2, y+2, offset, textColor, titleBarColor, title);
  219. }
  220.  
  221. void yakWindow::showActivated(void)
  222. {
  223.   showActivated(VisiblePageOffs);
  224.   if (VisiblePageOffs != HiddenPageOffs)
  225.     showActivated(HiddenPageOffs);
  226. }
  227.  
  228. void yakWindow::showDeActivated(word offset)
  229. {
  230.   x_rect_fill(x+2, y+2, x+width - 2, y+2+CharHeight, offset, titleBarColor-5);
  231.   x_bgprintf(x+2, y+2, offset, textColor-5, titleBarColor-5, title);
  232. }
  233.  
  234. void yakWindow::showDeActivated(void)
  235. {
  236.   showDeActivated(VisiblePageOffs);
  237.   if (VisiblePageOffs != HiddenPageOffs)
  238.     showDeActivated(HiddenPageOffs);
  239. }
  240.  
  241. word yakWindow::interpretMouseClick(void)
  242. {
  243.   if (isDragSelected())
  244.     drag();
  245.   if (isSizeSelected())
  246.     size();
  247.   if (isCloseSelected())
  248.     close();
  249.   return 0;
  250. }
  251.  
  252. word yakWindow::interpretKeyStroke(char myChar)
  253. {
  254.   return 0;
  255. }
  256.  
  257. void yakWindow::drag(void)
  258. {
  259.   int oldX = mouse.x();
  260.   int oldY = mouse.y();
  261.   int deltaX, deltaY;
  262.   mouse.hide();
  263.   while(mouse.isPressed(yakMouse::leftButton))
  264.   {
  265.     deltaX = mouse.x() - oldX;
  266.     deltaY = mouse.y() - oldY;
  267.     oldX = mouse.x();
  268.     oldY = mouse.y();
  269.     restore();
  270.     x += ((((int)x + deltaX) >= 0) && (((int)x + deltaX + width) <= ScrnLogicalPixelWidth)) ? deltaX : 0;
  271.     y += ((((int)y + deltaY) >= 0) && (((int)y + deltaY + height) <= ScrnLogicalHeight)) ? deltaY : 0;
  272.     save();
  273.     draw();
  274.   }
  275.   showActivated();
  276.   mouse.show();
  277. }
  278.  
  279. void yakWindow::size(void)
  280. {
  281.   int oldX = mouse.x();
  282.   int oldY = mouse.y();
  283.   int deltaX, deltaY;
  284.   mouse.hide();
  285.   while(mouse.isPressed(yakMouse::leftButton))
  286.   {
  287.     deltaX = mouse.x() - oldX;
  288.     deltaY = mouse.y() - oldY;
  289.     oldX = mouse.x();
  290.     oldY = mouse.y();
  291.     restore();
  292.     width  += (((width + deltaX) < (strlen(title)*CharWidth + 4)) || (((int)x + deltaX + width) > ScrnLogicalPixelWidth)) ? 0: deltaX;
  293.     bWidth = (width + (((4-(width % 4)) == 4) ? 0 : (4-(width%4))))/4;  //smallest mult of 4 > width!
  294.     height += (((height + deltaY) < (CharHeight + 4)) || (((int)y + deltaY + height) > ScrnLogicalHeight)) ? 0 : deltaY;
  295.     save();
  296.     draw();
  297.   }
  298.   showActivated();
  299.   mouse.show();
  300. }
  301.  
  302. int yakWindow::isSelected(void)
  303. {
  304.   if (mouse.isInBox(x, y, x + width, y + height) && mouse.isPressed(yakMouse::leftButton))
  305.     return 1;
  306.   else return 0;
  307. }
  308.  
  309. int yakWindow::isDragSelected(void)
  310. {
  311.   if (mouse.isInBox(x+2, y+2, x + width-2, y + 2 + CharHeight) && mouse.isPressed(yakMouse::leftButton) && (myFlags & isDraggable))
  312.     return 1;
  313.   else return 0;
  314. }
  315.  
  316. int yakWindow::isSizeSelected(void)
  317. {
  318.   if (mouse.isInBox(x+width-5, y+height-5, x+width, y+height) &&
  319.      !mouse.isInBox(x+width-5, y+height-5, x+width-3, y+height-3) &&
  320.      (myFlags & isSizeable))
  321.     return 1;
  322.   else return 0;
  323. }
  324.  
  325. int yakWindow::isCloseSelected(void)
  326. {
  327.   if (mouse.isInBox(x, y, x+5, y+5) &&
  328.      !mouse.isInBox(x+2, y+2, x+5, y+5)&&
  329.      (myFlags & isCloseable))
  330.     return 1;
  331.   else return 0;
  332. }
  333.  
  334. void yakWindow::selectToTop(void)
  335. {
  336.   yakWindow * myYakWindowPointer = topWindow;
  337.   while (myYakWindowPointer != NULL)
  338.   {
  339.     if (myYakWindowPointer->isSelected())
  340.       break;
  341.     myYakWindowPointer = myYakWindowPointer->prevWindow;
  342.   }
  343.   activeWindow = myYakWindowPointer;
  344.   mouse.hide();
  345.   if (activeWindow)
  346.   {
  347.     myYakWindowPointer->shuffleToTop();
  348.     activeWindow->showActivated();
  349.   }
  350.   else
  351.     topWindow->showDeActivated();
  352.   mouse.show();
  353. }
  354.  
  355. word yakWindow::advance(void)
  356. {
  357.   if (killPointer)
  358.   {
  359.     delete killPointer;
  360.     killPointer = NULL; //delete the "killed" window
  361.   }
  362.   if (mouse.isPressed(yakMouse::eitherButton))
  363.   {
  364. //    if (mouse.isPressed(yakMouse::leftButton))
  365.       selectToTop();
  366.     if (activeWindow)
  367.       return activeWindow->interpretMouseClick();
  368.   }
  369.   if (kbhit() && activeWindow)
  370.     return(activeWindow->interpretKeyStroke(getch()));
  371.   if (!(mouse.isPressed(yakMouse::eitherButton)))
  372.     mouse.isClicked(yakMouse::reset);
  373.   return 0;
  374. }